package com.qut.graduate.busrtquery.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.core.PoiInfo;
import com.baidu.mapapi.utils.DistanceUtil;
import com.qut.graduate.busrtquery.R;
import com.qut.graduate.busrtquery.interfaces.OnItemClickListener;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class StationQueryNearByAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static final String TAG = "StationQueryNearByAdapter";
    private Context mContext;
    private List<PoiInfo> mPoiInfoList;
    private LatLng currentLatLng;

    public StationQueryNearByAdapter(Context context, List<PoiInfo> poiInfoList) {
        this.mContext = context;
        this.mPoiInfoList = poiInfoList;
    }

    @Override
    public int getItemViewType(int position) {
        return 0;
    }

    @Override
    public int getItemCount() {
        return mPoiInfoList != null ? mPoiInfoList.size() : 0;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_station_query_nearby, parent, false);
        return new NearByHolder(view);
    }

    @SuppressLint("DefaultLocale")
    @Override
    public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder holder, int position) {
        if (mOnItemClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position = holder.getLayoutPosition();
                    mOnItemClickListener.onClick(holder.itemView, position);
                }
            });
        }
        NearByHolder suggestHolder = (NearByHolder) holder;
        suggestHolder.mPoiName.setText(mPoiInfoList.get(position).getName());
        suggestHolder.mPoiAddress.setText(mPoiInfoList.get(position).getAddress());
        if (currentLatLng!=null){
            double dis = DistanceUtil.getDistance(currentLatLng, mPoiInfoList.get(position).location);
            String distance = "";
            if (dis < 1000) {
                distance = String.valueOf((int)dis) + " m";
            } else {
                distance = String .format("%.2f",(dis / 1000)) + " km";
            }
            suggestHolder.mPoiDistance.setText(distance);
        }
    }

    public void updateDistance(LatLng latLng){
        currentLatLng = latLng;
        Collections.reverse(mPoiInfoList);
        notifyDataSetChanged();
    }

    public class NearByHolder extends RecyclerView.ViewHolder {
        ImageView mPoiIcon;
        TextView mPoiName;
        TextView mPoiAddress;
        TextView mPoiDistance;

        public NearByHolder(View view) {
            super(view);
            mPoiIcon = view.findViewById(R.id.item_nearby_icon);
            mPoiName = view.findViewById(R.id.item_nearby_name);
            mPoiAddress = view.findViewById(R.id.item_nearby_address);
            mPoiDistance = view.findViewById(R.id.item_nearby_distance);
        }
    }

    private OnItemClickListener mOnItemClickListener;

    public void setOnItemClickListener(OnItemClickListener OnItemClickListener) {
        this.mOnItemClickListener = OnItemClickListener;
    }

}

